import version from '../common/js/url.js';
export default {
	methods: {
		// 获取七牛云token
		getQiNiuToken() {
			this.$Ajax('/api/common/qiniu/getQiNiuToken', {}, res => {
				if(res.success){
					this.qiniu_token =  res.data.qiniu_token;
					this.qiniu_domain = res.data.qiniu_domain;
					uni.setStorageSync('qiniu_domain', this.qiniu_domain);
				}
			}, err => {})
		},
		// 查询用户信息
		getUserInfo() {
			this.$Ajax('/api/user/info/getRealUserInfo', {}, res => {
				if(res.success){
					this.usermsg = res.data.userInfo
					uni.setStorageSync('usermsg', this.usermsg);
					// console.log('usermsg',this.usermsg);
				}
			}, err => {uni.hideLoading();})
			uni.hideLoading();
		},
		// 返回上一页
		toback() {
			const pages = getCurrentPages() // eslint-disable-line
			// 有可返回的页面则直接返回，uni.navigateBack  默认返回失败之后会自动刷新页面 ，无法继续返回
			if (pages.length > 1) {
				uni.navigateBack(1)
				return
			}
			// vue router 可以返回uni.navigateBack失败的页面 但是会重新加载
			const a = this.$router.go(-1)
			// router.go失败之后则重定向到首页
			if (a == undefined) {
				uni.switchTab({
					url: '/pagesMain/index/index',
				})
			}

		},
		// 跳转页面
		goUrl(url, second = 0) {
			setTimeout(() => {
				uni.navigateTo({
					url,
					fail() {
						console.log('跳转失败,尝试Tab跳转,url: ' + url)
						uni.switchTab({
							url,
							fail() {
								console.log('Tab跳转失败,url: ' + url)
							},
						})
					},
				})
			}, second * 500)
		},

		/**
		 * 实现功能
		 * 将地址缩短 使用 ... 进行拼接
		 * 如下效果
		 * 0x0171...fB15d680
		 */
		textOmit(textValue, to, from) {
			let a = textValue.substring(0, to)
			let a2 = textValue.substring(textValue.length - from, textValue.length)
			return `${a}...${a2}`
		},
		// 获取日期
		getDate(seperator) {
			const date = new Date()
			const seperator1 = '-'
			const year = date.getFullYear()
			let month = date.getMonth() + 1
			let strDate = date.getDate()
			if (month >= 1 && month <= 9) {
				month = `0${month}`
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = `0${strDate}`
			}
			let currentdate = ''
			if(seperator){
				currentdate = year+ seperator + month + seperator + strDate
			}else{
				currentdate = year + month + strDate
			}
			
			return currentdate
		},
		// 获取当月第一天日期或最后一天
		getDateFirstLast(seperator,isLast) {
			const date = new Date()
			const seperator1 = '-'
			const year = date.getFullYear()
			let month = date.getMonth() + 1
			// let strDate = date.getDate()
			let strDate = 1
			// 使用示例
			var lastDay =  new Date(year, month , 0);
			lastDay = lastDay.getDate()
			if(isLast){
				strDate = lastDay
			}
			console.log('strDate',strDate); // 输出当前月的最后一天
			if (month >= 1 && month <= 9) {
				month = `0${month}`
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = `0${strDate}`
			}
			
			let currentdate = ''
			if(seperator){
				currentdate = year+ seperator + month + seperator + strDate
			}else{
				currentdate = year + month + strDate
			}
			
			return currentdate
		},
		// 消息提示
		msg(message, seconds = 2, obj = {}) {
			let data = {
				title: message,
				icon: 'none',
				duration: seconds * 1000,
			}
			if (obj.icon) {
				data.icon = obj.icon
			}
			uni.showToast(data)
			if (obj.url) {
				this.goUrl(obj.url, seconds)
			}
		},
		// 减法函数，用来得到精确的加法结果
		jian(arg2, arg1) {
		  var r1, r2, m, n;
		  try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
		  try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
		  m = Math.pow(10, Math.max(r1, r2));
		  //lastmodifybydeeka  
		  //动态控制精度长度  
		  n = (r1 >= r2) ? r1 : r2;
		  return ((arg2 * m - arg1 * m) / m).toFixed(n);
		},
		// 加法
		jia(arg1, arg2) {
		  var r1, r2, m;
		  try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
		  try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
		  m = Math.pow(10, Math.max(r1, r2));
		  return (arg1 * m + arg2 * m) / m;
		},
		// 乘法函数
		cheng(arg1, arg2) {
		  var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
		  try { m += s1.split(".")[1].length } catch (e) { }
		  try { m += s2.split(".")[1].length } catch (e) { }
		  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
		},
		// 除法函数
		chu(arg1, arg2) {
		  var t1 = 0, t2 = 0, r1, r2;
		  try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
		  try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
		  r1 = Number(arg1.toString().replace(".", ""))
		  r2 = Number(arg2.toString().replace(".", ""))
		  return (r1 / r2) * Math.pow(10, t2 - t1);
		},
		toFloor(value){
			return Math.floor(this.cheng(value,100)) / 100
		},
		// 图片地址完整显示
		imgShow(url){
			let imgUrl=''
			if(url){
				if(url.indexOf('http')>-1){
					imgUrl = url
				}else{
					const qiniu_domain = uni.getStorageSync('qiniu_domain') || '';
					imgUrl = qiniu_domain +url
				}
			}
			return imgUrl
		},
		//单位转化为k（千）或 w（万）
		viewCount(num) {
      // 1e3为10的三次幂=1000 1e4=10000
      // if (num >= 1e3 && num < 1e4) {
      //   return parseInt(num / 1e3)+ 'K'
      // }  
			if (num >= 1e4) {
        return this.handleDecimalPoint((num / 1e4)) + 'w'
      } else {
        return this.handleDecimalPoint(num)
      }
   	 },
		handleDecimalPoint(value, num = 2) {
			value = +value;
			if(!value) return 0;
			if(!Number.isInteger(value)) {
					value = +value.toFixed(num);
			}
			return value;
		},
		
	}
}

